// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/functions/lib/aggregates/min_nax_aggregate_base.h>

namespace kumo::pollux::functions::aggregate::sparksql {
    namespace {
        exec::AggregateRegistrationResult registerMin(
            const std::string &name,
            bool withCompanionFunctions,
            bool overwrite) {
            std::vector<std::shared_ptr<exec::AggregateFunctionSignature> > signatures;
            signatures.push_back(exec::AggregateFunctionSignatureBuilder()
                .orderableTypeVariable("T")
                .returnType("T")
                .intermediateType("T")
                .argumentType("T")
                .build());
            return exec::registerAggregateFunction(
                name,
                std::move(signatures),
                [name](
            core::AggregationNode::Step step,
            std::vector<TypePtr> argTypes,
            const TypePtr &resultType,
            const core::QueryConfig &config) -> std::unique_ptr<exec::Aggregate> {
                    auto factory = getMinFunctionFactory(
                        name,
                        CompareFlags::NullHandlingMode::kNullAsValue,
                        TimestampPrecision::kMicroseconds);
                    return factory(step, argTypes, resultType, config);
                },
                {false /*orderSensitive*/},
                withCompanionFunctions,
                overwrite);
        }

        exec::AggregateRegistrationResult registerMax(
            const std::string &name,
            bool withCompanionFunctions,
            bool overwrite) {
            std::vector<std::shared_ptr<exec::AggregateFunctionSignature> > signatures;
            signatures.push_back(exec::AggregateFunctionSignatureBuilder()
                .orderableTypeVariable("T")
                .returnType("T")
                .intermediateType("T")
                .argumentType("T")
                .build());
            return exec::registerAggregateFunction(
                name,
                std::move(signatures),
                [name](
            core::AggregationNode::Step step,
            std::vector<TypePtr> argTypes,
            const TypePtr &resultType,
            const core::QueryConfig &config) -> std::unique_ptr<exec::Aggregate> {
                    auto factory = getMaxFunctionFactory(
                        name,
                        CompareFlags::NullHandlingMode::kNullAsValue,
                        TimestampPrecision::kMicroseconds);
                    return factory(step, argTypes, resultType, config);
                },
                {false /*orderSensitive*/},
                withCompanionFunctions,
                overwrite);
        }
    } // namespace

    void registerMinMaxAggregates(
        const std::string &prefix,
        bool withCompanionFunctions,
        bool overwrite) {
        registerMin(prefix + "min", withCompanionFunctions, overwrite);
        registerMax(prefix + "max", withCompanionFunctions, overwrite);
    }
} // namespace kumo::pollux::functions::aggregate::sparksql
